package com.fresh.auth.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fresh.auth.Service.PermissionService;
import com.fresh.auth.mapper.PermissionMapper;
import com.fresh.entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public void add(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public List<Permission> getPermissions(Integer userId) {
        ArrayList<Integer> ridList = permissionMapper.getRoleId(userId);
        Integer roleId = Collections.min(ridList);
        ArrayList<Integer> pidList = permissionMapper.getPermissionId(roleId);
        List<Permission> permissions = new ArrayList<>();
        for (Integer i : pidList) {
            Permission permission = permissionMapper.selectOne(new QueryWrapper<Permission>().eq("permission_id",i));
            permissions.add(permission);
        }
        return permissions;
    }

    @Override
    public List<Permission> getAll() {
        return permissionMapper.selectList(null);
    }
}
